package com.polaris.live.adapter

import android.annotation.SuppressLint
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.ext.convertArabicDigitsToNormal
import com.polaris.live.databinding.AdapterMagicLampHistoryItemBinding
import com.polaris.live.resp.back_resp.PayerLampHistoryResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.image.ImageUtils

/**
 * MagicLampHistoryAdapter
 *
 * @author Created by 天晴 on 2024/11/27/008 15:01
 */
class MagicLampHistoryAdapter :
    BaseBindingAdapter<AdapterMagicLampHistoryItemBinding, PayerLampHistoryResp>() {

    @SuppressLint("SetTextI18n")
    override fun convert(
        holder: VBViewHolder<AdapterMagicLampHistoryItemBinding>,
        item: PayerLampHistoryResp,
    ) {
        holder.vb.apply {
            var color = ColorUtils.getColor(R.color.color_fed833)

            when (item.reward.lampType) {
                0 -> {
                    bgRl.setBackgroundResource(R.drawable.magic_lamp_copper_bg)
                    color = ColorUtils.getColor(R.color.color_ffdacd)
                }

                1 -> {
                    bgRl.setBackgroundResource(R.drawable.magic_lamp_silver_bg)
                    color = ColorUtils.getColor(R.color.color_e8e8e8)
                }

                2 -> {
                    bgRl.setBackgroundResource(R.drawable.magic_lamp_gold_bg)
                }
            }

            val user = item.anchorUser
            Glide.with(context)
                .load(ImageUtils.imgPath(item.reward.displayIcon))
                .into(imageRaw)
            itemUserNickName.text = item.reward.displayText
            itemUserNickName.setTextColor(color)
            rewardTips.text = item.reward.text
            timeTv.text = TimeUtils.millis2String(item.createTime, "MM-dd HH:mm").convertArabicDigitsToNormal()
            var name = if (user.nickname.length > 10) {
                user.nickname.substring(0, 10) + "..."
            } else {
                user.nickname
            }
            val fullText = StringUtils.getString(string.from_room, name)
            val spannableString = SpannableString(fullText)
            val startIndex = fullText.indexOf(name)
            if (startIndex != -1) {
                val endIndex = startIndex + name.length
                spannableString.setSpan(
                    ForegroundColorSpan(color),
                    startIndex,
                    endIndex,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                )
            }
            fromTv.text = spannableString
        }
    }
}